import 'dart:async';

import 'package:flutter/material.dart';

class StreamBuilderHomePage extends StatefulWidget {
  StreamBuilderHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _StreamBuilderHomePageState createState() => _StreamBuilderHomePageState();
}

class _StreamBuilderHomePageState extends State<StreamBuilderHomePage> {
  StreamController<bool> notifier;

  @override
  void initState() {
    super.initState();
    notifier = StreamController<bool>.broadcast();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: StreamBuilder<bool>(
          stream: notifier.stream,
          builder: (context, snapshot) {
            bool isInited = snapshot.data ?? false;

            return Stack(
              children: <Widget>[
                //顶部点击区
                Positioned(
                  top: 50.0,
                  left: 50.0,
                  child: Container(
                    color: isInited ? Colors.redAccent : Colors.green,
                    height: 100,
                    width: 100,
                  ),
                ),
                //底部点击区
              ],
            );
          }),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          notifier.add(true);
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
